﻿using KqAutoDesign.Models;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;

namespace KqAutoDesign.SonViews
{
    /// <summary>
    /// NewModule.xaml 的交互逻辑
    /// </summary>
    public partial class NewWorkflow : Window, INotifyPropertyChanged
    {
        public NewWorkflow()
        {
            InitializeComponent();
            Workflow = new WorkFlowBase();
            DataContext = this;
        }

        public NewWorkflow(int id, List<string> wfNames) :this()
        {
            Workflow.Id = id;
            if (wfNames != null)
            {
                wfNames.Add("无模板流程");
                BaseWorkflowNames = wfNames;
            }
            else
            {
                BaseWorkflowNames = new List<string>() { "无模板流程" };
            }
        }

        private List<string> _wfNames;
        public List<string> BaseWorkflowNames
        { 
            get
            {
                return _wfNames;
            }
            set
            {
                if (_wfNames != value)
                {
                    _wfNames = value;
                    RaisePropertyChanged(nameof(BaseWorkflowNames));
                }
            }
        }

        private string _baseWfName;
        public string BaseWfName
        {
            get { return _baseWfName; }
            set 
            {
                if (_baseWfName != value)
                {
                    _baseWfName = value;
                    RaisePropertyChanged(nameof(BaseWfName));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private WorkFlowBase _wf;
        public WorkFlowBase Workflow 
        {
            get {  return _wf; }
            set
            {
                if (_wf != value)
                {
                    _wf = value;
                    RaisePropertyChanged(nameof(Workflow));
                }
            }
        }


        private void OkBtnAction(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(Workflow.Name))
            {
                MessageBox.Show("工作流名称不能为空!", "提示", MessageBoxButton.OK);
                return;
            }
            if (string.IsNullOrEmpty(BaseWfName))
            {
                MessageBox.Show("基础流程不能为空!", "提示", MessageBoxButton.OK);
                return;
            }
            DialogResult = true;
            Close();
        }

        private void CancelBtnAction(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}
